<?php
function insert($table,$fields_arr)
{
	global $conn;
	if(!$conn){
	   return false;
    }
	$strfields = "";
	$strvalues = "";
	list($key, $val) = each($fields_arr);
	if(is_string($key)) {
		$strfields = " ($key";
		$strvalues= $val;
		while(list($key, $val) = each($fields_arr)){
			$strfields.= ", $key";
			$strvalues.= ",".$val;
		}
		$strfields.=")";
	}
    else {
		$strvalues=$fields_arr[0];
		for($i=1;$i<(count($fields_arr));$i++){
			$strvalues .= ", $fields_arr[$i]";
		}
	}
	
	$query = "INSERT INTO $table $strfields VALUES ($strvalues)";
	return mysql_query($query, $conn);
}

function update($table, $fields_arr, $where) 
{
	global $conn;
	if (!$conn) 
    { 
        return false; 
    }
	list($key, $val) = each($fields_arr);
	$strset=" $key = $val";
	while(list($key, $val) = each($fields_arr))
    {
		$strset .= ", $key = $val";
	}
	$query = "UPDATE $table SET $strset WHERE $where"; 
	$result = mysql_query($query, $conn);
	return !$result ? false : true;
}

function delete_rows($table, $fields_arr, $where_ext = "") 
{
	global $conn;
	if (!$conn) 
    { 
        return false; 
    }
	if(count($fields_arr) > 0)
    {
		list($key, $val) = each($fields_arr);
		$strwhere=" $key = $val";
		while(list($key, $val) = each($fields_arr))
        {
			$strwhere .= "OR $key = $val";
		}
	}
	
	$query = "DELETE FROM $table WHERE $strwhere $where_ext";      
	$result = mysql_query($query, $conn);
	if (!$result) 
    {
        return false;
    }
	return true;
}

function check_mail($email)
{
    if(!preg_match("/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i", $email)) 
        return false;
	return true;
}

function countRecord($table, $where = null)
{
    global $conn;
    if($table == null)
        return false;
    if($where == null)
        $where = "1 = 1";
    $result = mysql_query("Select count(*) as cnt From $table Where $where", $conn);
    $row = @mysql_fetch_assoc($result);
    return $row['cnt'];
}

function countPages($total, $n)
{
    if($total % $n == 0)
    {
        return (int)($total / $n);
    }
    return (int)($total / $n) + 1;
}

function createPage($total, $link, $nitem, $itemcurrent, $step = 10)
{
    if($total < 1)
    {
        return false;
    }
    $ret = "";
    $param = "";
    $pages = countPages($total, $nitem);
    if($itemcurrent > 0)
    {
        $ret .= '<a title="Next" href="'. $link . '0">[&lt;]</a>';
    }
    if($itemcurrent > 1)
    {
        $ret .= '<a title="Previous" href="'. $link . ($itemcurrent + 1) . '">[&lt;]</a>';
    }
    $from = ($itemcurrent - $step > 0 ? $itemcurrent - $step : 0);
    $to = ($itemcurrent + $step < $pages ? $itemcurrent + $step : $pages);
    for($i=$from; $i<$to; $i++)
    {
        if($i != $itemcurrent)
        {
            $ret .= '<a href="'. $link . $i .'">' . ($i + 1) . '</a>';
        }
        else
        {
            $ret .= '<b>' . ($i + 1) . '</b>';
        }
    }
    if(($itemcurrent < $pages - 2) && ($pages > 1))
    {
        $ret .= '<a title="Next" href="'. $link . ($itemcurrent + 1). '">[&gt;]</a> ';
    }
    if ($itemcurrent < $pages-1)
    {
        $ret .= '<a title="End" href="' .$link. ($pages-1). '">[&gt;&gt;]</a>';    
    }  
	return $ret;
}

function getFileExtension($filename)
{
    return strrchr($filename, ".");
}

function checkUpload($f, $ext="", $maxsize=0, $req=0)
{
    $fname = strtolower(basename($f['name']));
    $ftemp = $f['tmp_name'];
    $fsize = $f['size'];
    $fext = getFileExtension($fname);
    if($fsize == 0)
    {
        if($req == 0)
        {
            return "Chọn file upload";
        }
        return "";
    }
    else
    {
        if($ext != "")
        {
            if(strpos($ext, $fext) === false)
            {
                return "$fname không được để rỗng";
            }
            if($maxsize > 0) if($fsize > $maxsize)
            {
                return "Không thể ".$maxsize." byte";
            }
        }
    }
    return "";
}

function makeUpload($f, $newfile)
{
    if(move_uploaded_file($f['tmp_name'], $newfile))
    {
        return $newfile;
    }
    return false;
}

function getCategoryList()
{
    global $conn;
    $result = mysql_query("Select * From mid_categories", $conn);
    $ret = array();
    while($row = mysql_fetch_assoc($result))
    {
        $ret[] = $row;
    }
    return $ret;
}

function getListCategory($catid="",$split="=")
{
    global $con;
    $ret=array();
    if ($catid=="") $catid=0;
	$result = mysql_query("Select * from mid_categories where status=0 and parent_id=$catid",$con);
	while (($row=mysql_fetch_assoc($result)))
	{
		$ret[]=array($row['id'],($catid==0?"":$split).$row['categories_name']);
		$getsub=getListCategory($row['id'],$split.$split);
		foreach ($getsub as $sub)
			$ret[]=array($sub[0],$sub[1]);
	}
	return $ret;
}